package edu.cs.networkmonitor.utils;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

import org.jasypt.util.text.BasicTextEncryptor;

public class EmailUtil {

	public static void sendEmail(final String[] receivers, String mailSubject, String mailMessage) {
		if(receivers == null || receivers.length == 0) {
			return;
		}
		final String username = "netmonitoruwm@gmail.com";
    	BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
    	textEncryptor.setPassword("jencryt");
		final String password = textEncryptor.decrypt("VetGSQP3u60V/ImyOy1TVqBxZo0Ejd86");

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		Session session = Session.getInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});

		try {
            InternetAddress[] addressTo = new InternetAddress[receivers.length];
            for (int i = 0; i < receivers.length; i++)
            {
                addressTo[i] = new InternetAddress(receivers[i]);
            }
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(username));
			message.setRecipients(Message.RecipientType.TO, addressTo);
			message.setSubject(mailSubject);
			message.setText(mailMessage);

			Transport.send(message);

			System.out.println("Email sent to " + NetUtils.flattenArray(receivers));

		} catch (MessagingException e) {
			System.out.println(e.getMessage());
		}
	}

}
